package org.fing.edu.uy.admin.managedBean;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.fing.edu.uy.admin.config.MgrAdminConfig;
import org.fing.edu.uy.admin.factory.ManagerFactory;
import org.fing.edu.uy.admin.utils.FacesUtils;
import org.fing.edu.uy.esbadp.core.entity.DMRegisteredService;
import org.fing.edu.uy.esbadp.core.entity.KeyValuePair;
import org.fing.edu.uy.esbadp.core.entity.MetaDataConfig;

@ViewScoped
@ManagedBean(name = "serviceMetaController")
public class ServiceMetaController implements Serializable {

	private static final long serialVersionUID = 1L;

	private List<KeyValuePair> dataTable;
	private List<String> suggests;
	private KeyValuePair selectItem;
	private boolean editMode;
	private boolean newMode;
	private Long serviceID;
	private boolean selectView;
	private List<DMRegisteredService> services;

	/** Creates a new instance of ServiceController */
	public ServiceMetaController() {
		suggests = MgrAdminConfig.getInstance().getMetaSuggest();
		try {
			services = new LinkedList<DMRegisteredService>();
			serviceID = FacesUtils.getLongRequestParam("id");
			if (serviceID != null) {
				selectView = false;
			} else {
				selectView = true;
				services = ManagerFactory.getServiceManager().getAllVirtualServices();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@PostConstruct
	public void init() {
		try {
			if (serviceID != null) {
				dataTable = ManagerFactory.getServiceManager().getVirtualServiceMetadata(serviceID).attributesAsList();
			} else {
				dataTable = new LinkedList<KeyValuePair>();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public KeyValuePair getSelectItem() {
		return selectItem;
	}

	public void setSelectItem(KeyValuePair selectItem) {
		this.selectItem = selectItem;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}

	public boolean isNewMode() {
		return newMode;
	}

	public void setNewMode(boolean newMode) {
		this.newMode = newMode;
	}

	public List<KeyValuePair> getDataTable() {
		return dataTable;
	}

	public void setDataTable(List<KeyValuePair> dataTable) {
		this.dataTable = dataTable;
	}

	public List<String> complete(String query) {  
		List<String> result = new LinkedList<String>();
		for(String value : suggests){
			if(value.toLowerCase().contains(query.toLowerCase())){
				result.add(value);
			}
		}  
        return result;
    }  
	
	public void save() {
		if (selectItem != null) {
			MetaDataConfig aux = makeMetaConfig(selectItem);
			try {
				if (newMode) {
					ManagerFactory.getServiceManager()
							.persistVirtualServiceMetadata(aux, serviceID);
					FacesUtils.addInfo("The metadata was persisted");
				} else {
					ManagerFactory.getServiceManager().updateMetadata(selectItem, serviceID);
					FacesUtils.addInfo("The metadata was updated");
				}
				init();

			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} else {
			FacesUtils.addWarn("First select a record.");
		}
	}

	private MetaDataConfig makeMetaConfig(KeyValuePair item) {
		MetaDataConfig aux = new MetaDataConfig();
		aux.setAttribute(item.getKey(), item.getValue());
		return aux;
	}

	public void delete() {
		if (selectItem != null) {
			try {
				ManagerFactory.getServiceManager().deleteVirtualServiceMetadata(selectItem.getKey(),serviceID);
				init();
				FacesUtils.addInfo("Success");
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} else {
			FacesUtils.addWarn("First select a record.");
		}
	}

	public void create() {
		selectItem = new KeyValuePair();
	}

	public void reload() {
		init();
	}

	public Long getServiceID() {
		return serviceID;
	}

	public void setServiceID(Long serviceID) {
		this.serviceID = serviceID;
	}

	public boolean isSelectView() {
		return selectView;
	}

	public void setSelectView(boolean selectView) {
		this.selectView = selectView;
	}

	public List<DMRegisteredService> getServices() {
		return services;
	}

	public void setServices(List<DMRegisteredService> services) {
		this.services = services;
	}

}
